﻿using System;
using System.Collections.Generic;
using System.Text;
using RouterControl.IO;

namespace RouterControl.Service
{
	public sealed class RouterDeviceFinder : SingletonDeviceFinder
	{
		private static RouterDeviceFinder _instance = new RouterDeviceFinder();

		public static RouterDeviceFinder Instance
		{
			get { return _instance; }
		}

		private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(RouterDeviceFinder));

		/// <summary>
		/// router selected by the user's preferences
		/// </summary>
		/// <remarks>can block</remarks>
		public RootDevice SelectedRouter
		{
			get
			{
				var n = Properties.Settings.Default.SelectedRouterDeviceUniqueName;

				if (string.IsNullOrEmpty(n))
					return null;

				// make sure we searched for it
				EnsureFirstSearchAndWait();

				// retrieve router
				return GetDeviceById(n) as RootDevice;
			}
		}

		private RouterDeviceFinder()
			: base(
                new[] { IO.RootDeviceTypeIds.InternetGatewayDeviceType, IO.RootDeviceTypeIds.InternetGatewayDslDeviceType }, 
                true
                )
		{
			if (_log.IsDebugEnabled)
				_log.Debug("Creating singleton");

			try
			{
				
			}
			catch (Exception e)
			{
				_log.Fatal("Failed to create singleton", e);

				throw;
			}

		}
	}
}
